home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / Frontier 4.0.1 / Frontier SDK 4.0b2 / Sample Code / MenuSharer Program / main.c next >
Encoding:
C/C++ Source or Header  |  1996-01-18  |  10.3 KB  |  579 lines  |  [TEXT/CWIE]

  1.  
  2. /*© copyright 1991-96 UserLand Software, Inc. All Rights Reserved.*/
  3.  
  4.  
  5. /*
  6. Briefly -- this is a very small program that demonstrates the use of the 
  7. Menu Sharing Toolkit. For detailed info, see the enclosed readme file.
  8.  
  9. You can search the source code for menu sharing highlights by searching 
  10. for the following string: "See Step #". The step numbers refer to steps 
  11. in the "Cookbook" section of the enclosed readme.
  12.  
  13. We hope you like menu sharing and add it to your Macintosh application.
  14.  
  15. Thanks!
  16. */
  17.  
  18. #if !defined (THINK_C) || !defined (__MWERKS__)
  19.     
  20.     #include <Menus.h>
  21.     #include <Windows.h>
  22.     #include <Events.h>
  23.     #include <Fonts.h>
  24.     
  25. #endif
  26.  
  27. #include <menusharing.h> /*See Step #5*/
  28.  
  29.  
  30. #define applemenu 128 /*the resource id of the apple menu*/
  31.  
  32. #define aboutitem 1 /*the about command*/
  33.  
  34. #define filemenu 129 /*resource id of file menu, shared menus appear to right of this*/
  35.  
  36. #define firstsharedmenu filemenu+1 /*resource id for first shared menu*/
  37.  
  38. #define quititem 1 /*the single item in the file menu -- this is a very simple program!*/
  39.  
  40. MenuHandle happlemenu, hfilemenu; /*the two fixed, non-shared menus in this program*/
  41.  
  42. WindowPtr mainwindow = nil; /*the menu sharing test window*/
  43.  
  44. Str255 windowmessage; /*the message that's displayed in the main window*/
  45.  
  46. Boolean flexitmainloop = false; /*when true we fall thru the main event loop*/
  47.  
  48. Boolean flcurrentlyactive = true;
  49.  
  50.  
  51.  
  52.  
  53. static void copystring (Str255 source, Str255 dest) {
  54.  
  55.     /*
  56.     create a copy of source in dest.  copy the length byte and
  57.     all the characters in the source string.
  58.  
  59.     assume the strings are pascal strings, with the length byte in
  60.     the first character of the string.
  61.     */
  62.  
  63.     short i, len;
  64.     
  65.     len = (short) source [0];
  66.     
  67.     for (i = 0; i <= len; i++) 
  68.         dest [i] = source [i];
  69.     } /*copystring*/
  70.  
  71.  
  72. static void ellipsize (Str255 s, short width) {
  73.  
  74.     /*
  75.     if the string fits inside the given number of pixels, fine -- do nothing
  76.     and return.
  77.     
  78.     if not, return a string that does fit, with ellipses representing the 
  79.     deleted characters.  ellipses are generated by pressing option-semicolon.
  80.     */
  81.     
  82.     char len;
  83.     short newwidth;
  84.     
  85.     if ((newwidth = StringWidth (s)) <= width) /*nothing to do, the string fits*/
  86.         return;
  87.     
  88.     len = s [0]; /* current length in characters*/
  89.     
  90.     width -= CharWidth ('…'); /* subtract width of ellipses*/
  91.         
  92.     do { /*until it fits (or we run out of characters)*/
  93.     
  94.         newwidth -= CharWidth (s [len]);
  95.         
  96.         --len;
  97.     } while ((newwidth > width) && (len != 0));
  98.     
  99.     ++len; /*make room for the ellipses*/
  100.     
  101.     s [len] = '…'; 
  102.     
  103.     s [0] = (char) len;
  104.     } /*ellipsize*/
  105.  
  106.  
  107. static void centerstring (Rect r, Str255 s) {
  108.     
  109.     /*
  110.     draw the string in the current font, size and style, centered inside
  111.     the indicated rectangle.
  112.     */
  113.     
  114.     short rh = r.bottom - r.top;
  115.     short rw = r.right - r.left;
  116.     short h, v;
  117.     FontInfo fi;
  118.     
  119.     GetFontInfo (&fi);
  120.     
  121.     ellipsize (s, rw); /*make sure it fits inside the rectangle, width-wise*/
  122.     
  123.     h = r.left + ((rw - StringWidth (s)) / 2);
  124.     
  125.     v = r.top + ((rh - (fi.ascent + fi.descent)) / 2) + fi.ascent;
  126.     
  127.     MoveTo (h, v);
  128.     
  129.     ClipRect (&r);
  130.     
  131.     DrawString (s);
  132.     } /*centerstring*/
  133.  
  134.  
  135. static void setfontsizestyle (short fontnum, short fontsize, short fontstyle) {
  136.  
  137.     TextFont (fontnum);
  138.     
  139.     TextSize (fontsize);
  140.     
  141.     TextFace (fontstyle);
  142.     } /*setfontsizestyle*/
  143.     
  144.     
  145. static void updatemainwindow (void) {
  146.     
  147.     Rect r;
  148.     Str255 s;
  149.     
  150.     r = (*mainwindow).portRect;
  151.     
  152.     EraseRect (&r);
  153.     
  154.     setfontsizestyle (helvetica, 12, 0);
  155.     
  156.     centerstring (r, windowmessage);
  157.     
  158.     NumToString (FreeMem () / 1024, s);
  159.     
  160.     MoveTo (r.left + 3, r.bottom - 3);
  161.     
  162.     setfontsizestyle (geneva, 9, 0);
  163.     
  164.     DrawString (s);
  165.     
  166.     DrawString ("\pK");
  167.     } /*updatemainwindow*/
  168.     
  169.     
  170. static Boolean setwindowmessage (Str255 s) {
  171.     
  172.     copystring (s, windowmessage);
  173.     
  174.     SetPort (mainwindow);
  175.     
  176.     updatemainwindow ();
  177.     
  178.     return (true);
  179.     } /*setwindowmessage*/
  180.  
  181.  
  182. static Boolean initmainwindow (void) {
  183.     
  184.     mainwindow = GetNewWindow (128, nil, (WindowPtr) -1);
  185.     
  186.     if (mainwindow == nil)
  187.         return (false);
  188.     
  189.     ShowWindow (mainwindow);
  190.     
  191.     return (true);
  192.     } /*initmainwindow*/
  193.  
  194.  
  195. static void handleupdate (EventRecord *ev) {
  196.     
  197.     WindowPtr w;
  198.     
  199.     w = (WindowPtr) (*ev).message;
  200.     
  201.     BeginUpdate (w);
  202.     
  203.     SetPort (w);
  204.     
  205.     updatemainwindow ();
  206.     
  207.     EndUpdate (w);
  208.     } /*handleupdate*/
  209.  
  210.  
  211. static void handledrag (EventRecord *ev, WindowPtr w) {
  212.     
  213.     Rect r;
  214.  
  215.     r = qd.screenBits.bounds; 
  216.     
  217.     r.top = r.top + GetMBarHeight (); 
  218.     
  219.     InsetRect (&r, 4, 4);
  220.     
  221.     DragWindow (w, (*ev).where, &r);
  222.     } /*handledrag*/
  223.  
  224.  
  225. static void handlemenu (long codeword) {
  226.     
  227.     short idmenu, iditem;
  228.     
  229.     iditem = LoWord (codeword);
  230.     
  231.     idmenu = HiWord (codeword);
  232.     
  233.     if (SharedMenuHit (idmenu, iditem)) /*See Step #3*/    
  234.         goto exit;
  235.     
  236.     switch (idmenu) {
  237.     
  238.         case applemenu: 
  239.             switch (iditem) {
  240.                 
  241.                 case aboutitem:
  242.                     Alert (262, nil);
  243.                     
  244.                     break;
  245.             
  246.                 default: {
  247.                     Str255 s;
  248.                     
  249.                     GetItem (happlemenu, iditem, s);
  250.                     
  251.                     OpenDeskAcc (s);
  252.                     
  253.                     break;
  254.                     }
  255.                 } /*switch*/
  256.             
  257.             break; /*apple menu*/
  258.  
  259.         case filemenu: 
  260.             switch (iditem) {
  261.                 
  262.                 case quititem:
  263.                 
  264.                     flexitmainloop = true;
  265.                     
  266.                     break;
  267.                 } /*switch*/
  268.             
  269.             break; /*file menu*/
  270.             
  271.         } /*switching on which menu was invoked*/
  272.         
  273.     exit:
  274.     
  275.     HiliteMenu (0);
  276.     } /*handlemenu*/
  277.  
  278.  
  279. static void handlemouse (EventRecord *ev) {
  280.  
  281.     short part;
  282.     WindowPtr w;
  283.     
  284.     part = FindWindow ((*ev).where, &w);
  285.     
  286.     if (w != nil) 
  287.     
  288.         if (w != FrontWindow ()) { /*just like all other Mac programs*/
  289.             
  290.             SelectWindow (w);
  291.                             
  292.             return; /*the mouse click is consumed by the bringtofront operation*/
  293.             }
  294.     
  295.     switch (part) {
  296.     
  297.         case inMenuBar: 
  298.             handlemenu (MenuSelect ((*ev).where)); 
  299.             
  300.             break;
  301.         
  302.         case inSysWindow:
  303.             SystemClick (ev, w); 
  304.             
  305.             break;
  306.         
  307.         case inDrag:
  308.             handledrag (ev, w);
  309.             
  310.             break;
  311.             
  312.         } /*switch*/
  313.     } /*handlemouse*/
  314.  
  315.  
  316. static void handlekeystroke (EventRecord *ev) { /*See Step #4*/
  317.  
  318.     char ch = (*ev).message & charCodeMask;
  319.     
  320.     if (SharedScriptRunning ()) { /*cmd-period terminates the script*/
  321.     
  322.         if (((*ev).modifiers & cmdKey) && (ch == '.')) { 
  323.             
  324.             CancelSharedScript (); /*cancel the shared menu script, if one is running*/
  325.         
  326.             return;
  327.             }
  328.         }
  329.     
  330.     handlemenu (MenuKey (ch)); /*not cmd-period, process the normal way*/
  331.     } /*handlekeystroke*/
  332.  
  333.  
  334. static void handlejuggle (EventRecord *ev) {
  335.     
  336.     flcurrentlyactive = ((*ev).message & resumeFlag) != 0;
  337.     } /*handlejuggle*/
  338.  
  339.  
  340. static void handleevent (EventRecord *ev) {
  341.     
  342.     switch ((*ev).what) {
  343.     
  344.         case keyDown: case autoKey: 
  345.             handlekeystroke (ev);
  346.             
  347.             break;
  348.             
  349.         case mouseDown:
  350.             handlemouse (ev);
  351.             
  352.             break;
  353.         
  354.         case updateEvt:
  355.             handleupdate (ev);
  356.             
  357.             break;
  358.         
  359.         case osEvt:
  360.             handlejuggle (ev);
  361.             
  362.             break;
  363.         
  364.         case kHighLevelEvent:
  365.             AEProcessAppleEvent (ev);
  366.             
  367.             break;
  368.         
  369.         case nullEvent: /*See Step #2*/
  370.             if (flcurrentlyactive)
  371.                 CheckSharedMenus (firstsharedmenu);
  372.             
  373.             break;
  374.         } /*switch*/
  375.     } /*handleevent*/
  376.  
  377.  
  378. static void maineventloop (void) {
  379.     
  380.     EventRecord ev;
  381.     
  382.     while (!flexitmainloop) {
  383.         
  384.         WaitNextEvent (everyEvent, &ev, 1, nil);
  385.         
  386.         handleevent (&ev);
  387.         } /*while*/
  388.     } /*maineventloop*/
  389.  
  390.  
  391. static void initmenus (void) {
  392.     
  393.     /*
  394.     set up our apple and file menus.
  395.     */
  396.     
  397.     happlemenu = GetMenu (applemenu); 
  398.     
  399.     AddResMenu (happlemenu, 'DRVR'); 
  400.     
  401.     InsertMenu (happlemenu, 0); 
  402.     
  403.     hfilemenu = GetMenu (filemenu); 
  404.     
  405.     InsertMenu (hfilemenu, 0);
  406.     
  407.     DrawMenuBar ();
  408.     } /*initmenus*/
  409.  
  410.  
  411. static void initmacintosh (void) {
  412.  
  413.     /*
  414.     the magic stuff that every Macintosh application needs to do 
  415.     before doing anything else.
  416.     */
  417.  
  418.     short i;
  419.         
  420.     MaxApplZone ();
  421.     
  422.     for (i = 0; i < 10; i++) 
  423.         MoreMasters ();
  424.     
  425.     InitGraf (&qd.thePort);
  426.     
  427.     InitFonts ();
  428.     
  429.     FlushEvents (everyEvent, 0);
  430.     
  431.     InitWindows ();
  432.     
  433.     InitMenus ();
  434.     
  435.     TEInit ();
  436.     
  437.     InitDialogs (0L);
  438.     
  439.     InitCursor ();
  440.     
  441.     for (i = 0; i < 5; i++) { /*register with Multifinder*/
  442.         
  443.         EventRecord ev;
  444.         
  445.         EventAvail (everyEvent, &ev); /*see TN180 -- splash screen*/
  446.         } /*for*/
  447.     } /*initmacintosh*/
  448.  
  449.  
  450. static pascal OSErr handlequit (const AppleEvent *event, AppleEvent *reply, long refcon) {
  451.     
  452.     #pragma unused (event, reply, refcon)
  453.  
  454.     flexitmainloop = true;
  455.     
  456.     return (noErr);
  457.     } /*handlequit*/
  458.     
  459.     
  460. static pascal OSErr handleopenapp (const AppleEvent *event, AppleEvent *reply, long refcon) {
  461.     
  462.     #pragma unused (event, reply, refcon)
  463.  
  464.     return (noErr);
  465.     } /*handleopenapp*/
  466.  
  467.  
  468. static pascal OSErr handleopen (const AppleEvent *event, AppleEvent *reply, long refcon) {
  469.     
  470.     #pragma unused (event, reply, refcon)
  471.  
  472.     return (noErr);
  473.     } /*handleopen*/
  474.     
  475.         
  476. static pascal OSErr handleprint (const AppleEvent *event, AppleEvent *reply, long refcon) {
  477.     
  478.     #pragma unused (event, reply, refcon)
  479.  
  480.     return (noErr);
  481.     } /*handleprint*/
  482.     
  483.         
  484. static pascal OSErr setmessageverb (const AppleEvent *event, AppleEvent *reply, long refcon) {
  485.  
  486.     #pragma unused (refcon)
  487.  
  488.     OSErr ec;
  489.     AEDesc result;
  490.     Str255 s;
  491.     Handle htext;
  492.     long lentext;
  493.     Boolean fl;
  494.     
  495.     ec = AEGetParamDesc (event, keyDirectObject, typeChar, &result);
  496.     
  497.     if (ec != noErr) 
  498.         return (ec);
  499.         
  500.     htext = result.dataHandle;
  501.     
  502.     if (htext == nil)
  503.         return (noErr);
  504.         
  505.     lentext = GetHandleSize (htext);
  506.     
  507.     if (lentext > 255)
  508.         lentext = 255;
  509.         
  510.     s [0] = (unsigned char) lentext;
  511.     
  512.     BlockMove (*htext, &s [1], lentext);
  513.     
  514.     AEDisposeDesc (&result);
  515.     
  516.     setwindowmessage (s);
  517.     
  518.     fl = true;
  519.     
  520.     ec = AEPutParamPtr (reply, keyDirectObject, typeBoolean, (Ptr) &fl, sizeof (Boolean));
  521.     
  522.     return (ec);
  523.     } /*setmessageverb*/
  524.     
  525.  
  526. static void installhandlers (void) {
  527.  
  528.     AEInstallEventHandler ('TEST', 'smsg', NewAEEventHandlerProc (setmessageverb), 0, false);
  529.  
  530.     AEInstallEventHandler (kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc (handleopenapp), 0, false);
  531.     
  532.     AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc (handleopen), 0, false);
  533.     
  534.     AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc (handleprint), 0, false);
  535.     
  536.     AEInstallEventHandler (kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc (handlequit), 0, false);
  537.     } /*installhandlers*/
  538.     
  539.     
  540. static pascal void errordialog (Str255 s) {
  541.     
  542.     ParamText (s, "\p", "\p", "\p"); 
  543.     
  544.     Alert (263, nil);
  545.     } /*errordialog*/
  546.     
  547.     
  548. static pascal void eventfilter (EventRecord *ev) {
  549.     
  550.     handleevent (ev); /*could receive an update, activate, OS, or null event*/
  551.     } /*eventfilter*/
  552.  
  553.  
  554. void main (void) {
  555.     
  556.     initmacintosh ();
  557.     
  558.     if (!InitSharedMenus (errordialog, eventfilter)) /*See Step #1*/
  559.         goto error;
  560.         
  561.     installhandlers ();
  562.         
  563.     initmenus ();
  564.     
  565.     initmainwindow ();
  566.     
  567.     maineventloop ();
  568.         
  569.     ExitToShell ();
  570.     
  571.     error:
  572.     
  573.     Alert (261, nil); /*this application requires system 7.0 or higher*/
  574.         
  575.     ExitToShell ();
  576.     } /*main*/
  577.  
  578.  
  579.